package com.ay277.htqoa.mail;

import org.springframework.mail.MailException;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.net.Authenticator;
import java.net.PasswordAuthentication;
import java.util.Properties;

/**
 * Created by accp09 on 2016/12/23.
 */
public class SendMail  {
        public void sendMail(){
        final Properties props=new Properties();
        props.put("mail.smtp.auth","true");
        props.put("mail.smtp.host","smtp.163.com");
        props.put("mail.transport.protocol","smtp");
        props.put("mail.user","13651168620@163.com");

        props.put("mail.password","yanjintao1");
        props.put("mail.smtp.ssl.enable", "true");
        props.put("mail.smtp.port", 465);

        Session session=Session.getInstance(props, new javax.mail.Authenticator() {
            @Override
            protected javax.mail.PasswordAuthentication getPasswordAuthentication() {
                return new javax.mail.PasswordAuthentication("13651168620@163.com","yanjintao1");
            }
        });
        MimeMessage mimeMessage=new MimeMessage(session);
        InternetAddress from= null;
        try {
            from = new InternetAddress(props.getProperty("mail.user"));
            mimeMessage.setFrom(from);
            InternetAddress to=new InternetAddress("626423058@qq.com");
            mimeMessage.setRecipient(Message.RecipientType.TO,to);
            mimeMessage.setSubject("java mail 作業已發送 ");
            mimeMessage.setText("郵箱轟炸機");
            Transport.send(mimeMessage);
            System.out.println("發送完畢");
        } catch (Exception e) {
            e.printStackTrace();
        }

    }


}
